/* ---------------------------------广告轮播开始---------------------------------- */
function _as_loadjs(url) {
	_doc = document.getElementsByTagName('head')[0];
	var js = document.createElement('script');
	js.setAttribute('type', 'text/javascript');
	js.setAttribute('src', url);
	_doc.appendChild(js);
	return js;
}

function _as_turn(key, pool) {
	this.ad_pool = [];
	this.key = '_as_turn';

	if (key) this.key = key;
	if (pool && pool.length)
		this.ad_pool = pool;
}

_as_turn.prototype.add = function(jsurl, weight) {
	if (jsurl) this.ad_pool.push({jsurl: jsurl, weight: weight?weight:1});
};

_as_turn.prototype.getcv = function(k) {
	var c = document.cookie
	var end = c.indexOf (k);
	if (end == -1)
		return null;
	var st = end+k.length+1;
	end = c.indexOf(";", st);
	if (end == -1)
		end = c.length;
	return unescape(c.substring(st, end));
};
_as_turn.prototype.setcv = function(k, v, e, p, d, s) {
	var expdate = new Date();
	var argv = arguments;
	var argc = arguments.length;
	var expires = (argc>2)?argv[2]:null;
	var path = (argc>3)?argv[3]:null;
	var domain = (argc>4)?argv[4]:null;
	var secure = (argc>5)?argv[5]:false;
	if(expires!=null)
		expdate.setTime(expdate.getTime()+(expires*1000));
	var c =  k+"="+escape(v)+"; ";
	c += ((expires==null)?"":("expires="+expdate.toGMTString()+"; "));
	c += ((path==null)?"":("path="+path+"; "))
	c += ((domain==null)?"":("domain="+domain+"; "))
	c += ((secure==true)?"secure; ":"");
	document.cookie = c;
};
_as_turn.prototype.delcv = function(k) {
	var exp = new Date();
	exp.setTime(exp.getTime()-1);
	var v = _as_getcv(k);
	document.cookie = k+"="+v+"; expires="+exp.toGMTString();
};

_as_turn.prototype.reset_turn = function(pool) {
	var reval = '';

	if (!pool) pool = this.ad_pool;

	if (pool.length) {
		var cur = parseInt(Math.random()*100)%pool.length;
		if (!pool[cur].weight)pool[cur].weight = 1;

		for (var i=0; i<pool.length; i++) {
			if (!pool[i].weight || pool[i].weight<0)
				pool[i].weight = 1;

			if (cur==i)
				reval += (reval?',':'')+(parseInt(pool[i].weight)-1);
			else
				reval += (reval?',':'')+pool[i].weight;
		}
	}

	this.setcv(this.key, cur+','+reval);
	return cur;
};

_as_turn.prototype.check_turn = function(pool) {
	if (!pool) pool = this.ad_pool;
	var turn = this.getcv(this.key);

	if (!turn) return false;
	var ary = turn.split(',');
	if (!pool.length || ary.length != (pool.length+1))
		return false;
	return turn;
};

_as_turn.prototype.next_turn = function(pool, cookie) {
	if (!pool) pool = this.ad_pool;
	if (!cookie) cookie = this.getcv(this.key);
	var ary = cookie.split(',');
	var cur = parseInt(ary[0]);
	var ocur = parseInt(ary[0]);

	cur = (cur+1)%pool.length;
	while (ary[cur+1]<=0) {
		if (cur==ocur) return -1;
		cur = (cur+1)%pool.length;
	}
	ary[cur+1]--;
	var reval = '';
	for (var i=1; i<ary.length; i++) {
		reval += (reval?',':'')+ary[i];
	}
	this.setcv(this.key, cur+','+reval);
	return cur;
};

_as_turn.prototype.run = function() {
    var pool = this.ad_pool;
	var turn = this.check_turn(pool);

	if (!turn) {
		var cur = this.reset_turn(pool);
	} else {
		var cur = this.next_turn(pool, turn);
		if (cur<0) {
			var cur = this.reset_turn(pool);
		}
	}

	if (pool[cur].jscall)
		eval(pool[cur].jscall);
	else if (pool[cur].jsurl)
		_as_loadjs(pool[cur].jsurl);
	return turn;
};
/* ---------------------------------广告轮播结束---------------------------------- */

/* ---------------------------------广告队列开始---------------------------------- */
function _as_queue(queue) {
	this.kary = [];
	this.queue = {"_1": [], "_2": [], "_3": [], "_4": [], "_5": []};

	if (queue) this.queue = queue;
}

_as_queue.prototype.add = function(callback, index, seconds) {
	if (typeof callback != 'function')
		return false;

	var q = this.queue;
	if (!q["_"+index]) return false;
	var len = q[index].push({c:callback, s:seconds, o:0});
	return q[index][len-1];
};

_as_queue.prototype.init = function() {
	var q = this.queue;

	for (var k in q) {
		if (q[k].length>0) this.kary.push(k);
	}
};

_as_queue.prototype.next = function(index) {
	var q = this.queue;
	var k = this.kary[++index];

	while (q[k] && q[k].length>0) {return q[k];}
	return null;
};

_as_queue.prototype.run = function() {
	var q = this.queue;
	var hd = null;
	var j = 0; var i = -1;
	var k = null; var len = 0;
	var obj = this;

	this.init();
	k = this.next(i++);
	if (!k) return;
	len = k.length;
	k[0].c();

	hd = window.setInterval(function() {
		if (k[j].o) {
			j++;
			if (j<len) {k[j].c();}
			else {
				k = obj.next.call(obj, i++);
				if (!k) {window.clearInterval(hd);}
				else {
					len = k.length;
					j = 0;
					k[j].c();
				}
			}
		}
	}, 1000);
}
/* ---------------------------------广告队列结束---------------------------------- */

/* ---------------------------------广告排头开始---------------------------------- */
function _as_firstavailable() {
	this.ad_pool = [/*{jsurl:'', jscall:'', priority:''}*/];

	this.flag = false;
	this.loaded = false;
}

_as_firstavailable.prototype.add = function(jsurl, priority) {
	var pool = this.ad_pool;

	for (var i=0; i<pool.length; i++) {
		if (pool[i].priority<priority) {
			break;
		}
	}

	if (i==pool.length) {
		pool.push({jsurl:jsurl, priority:priority});
	} else {
		for (var j=pool.length; j>i; j--) {
			pool[j] = pool[j-1];
		}
		pool[j] = {jsurl:jsurl, priority:priority};
	}
};

_as_firstavailable.prototype.isIe = function() {
	return navigator.userAgent.toLowerCase().indexOf("msie")!=-1;
};

_as_firstavailable.prototype.load = function(jsobj) {
	var obj = this;
	obj.flag = false;

	if (typeof jsobj.jscall=='function') {
		jsobj.jscall();
		obj.loaded = true;
	} else {
		obj.loaded = false;
		var js = _as_loadjs(jsobj.jsurl);

		if (!this.isIe()) {
			js.onload = function () {
				obj.loaded = true;
			}
		} else {
			js.onreadystatechange = function () {
				if (js.readyState == 'loaded' 
					|| js.readyState == 'complete') {
					obj.loaded = true;
				}
			}
		}
	}
};

_as_firstavailable.prototype.isloaded = function() {
	return this.loaded;
};

_as_firstavailable.prototype.setflag = function(state) {
	this.flag = state;
};

_as_firstavailable.prototype.check = function() {
	return this.flag;
};

_as_firstavailable.prototype.run = function() {
	var pool = this.ad_pool;
	var obj = this;

	var len = pool.length;
	var i = 0;
	obj.load(pool[i++]);

	var hd = window.setInterval(function() {
		if (obj.isloaded()) {
			if (obj.check()) {window.clearInterval(hd);}
			else {
				if (i<len) {obj.load(pool[i++]);}
				else {window.clearInterval(hd);}
			}
		}
	}, 1000);
};
/* ---------------------------------广告排头开始---------------------------------- */
